package com.xxz.terminal;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Calendar;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "login";
    EditText uid,pwd;
    Button login,register;
    DBHelper dbHelper;
    SQLiteDatabase db;
    String nickname;
    String motto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        uid = findViewById(R.id.user_id);
        pwd = findViewById(R.id.password);

        dbHelper = new DBHelper(this);
        db = dbHelper.getWritableDatabase();

        SharedPreferences sp = getSharedPreferences("userinformation", Activity.MODE_PRIVATE);
        uid.setText(sp.getString("user_id",null));//填充已经注册的用户名
        pwd.setText(sp.getString("password",null));//填充已经注册的密码

        }

        @Override
       protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

                if (requestCode == 1 && resultCode == 2) {
                    Bundle bundle = data.getExtras();

                    //将注册的数据写道sp里
                    SharedPreferences sp = getSharedPreferences("userinformation", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("user_id", bundle.getString("user_id", null));
                    editor.putString("password", bundle.getString("password", null));
//                    editor.putString("nickname",bundle.getString("nickname", null));
//                    editor.putString("motto",bundle.getString("motto", null));
                    editor.apply();

                    Log.i(TAG, "onActivityResult: 数据已保存到sharedPreferences");//将当前登录者的信息做初始保存，昵称和格言两个字段给予初始设定

                    uid.setText(sp.getString("user_id",null));//填充已经注册的用户名
                    pwd.setText(sp.getString("password",null));//填充已经注册的密码
                }
            }

    @SuppressLint("Range")
    public void login(View btn){

        String user_id = uid.getText().toString();
        String password = pwd.getText().toString();

        Cursor cursor = db.query("userinformation",new String[]{"user_id","password"},"user_id =? and password =? "
                , new String[]{user_id,password},null,null,null);//查询与输入框中值相同的记录，即id和密码
            if(cursor.getCount() == 0){
                Toast.makeText(this, "账号不存在或密码错误", Toast.LENGTH_SHORT).show();
            }
            else{

                cursor = db.query("userinformation",new String[]{"nickname","motto"},"user_id=?"
                        , new String[]{user_id},null,null,null);//查询data表中当前用户的日记和日期

                cursor.moveToFirst();
                    nickname = cursor.getString(cursor.getColumnIndex("nickname"));
                    motto = cursor.getString(cursor.getColumnIndex("motto"));
                //默认为-1，要先让游标回到第一行
                cursor.close();
//                Toast.makeText(this, nickname, Toast.LENGTH_SHORT).show();

                //将登录框中的数据写到sp,将获得的昵称，格言也放入sp，方便后面调用
                SharedPreferences sp = getSharedPreferences("userinformation", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("user_id", user_id);
                editor.putString("password", password);
                editor.putString("nickname",nickname);
                editor.putString("motto",motto);
                editor.apply();

                Log.i(TAG, "onActivityResult: 数据已保存到sharedPreferences");
                Log.i(TAG, "登录成功");
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,MainPageActivity.class);

                startActivity(intent);
                finish();
            }//登录成功打开主页
        }//触发登录按钮

        public void register(View btn){

            Intent intent = new Intent(this,RegisterActivity.class);
            //startActivity(intent);
            //setResult(1,intent);
            startActivityForResult(intent,1);

        }//触发注册
    }



